package com.bdqn.ch09.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping(value = "/dologin")
    public String dologin(String usrName, String usrPassword, HttpSession request) {
        User user = userService.login(usrName, usrPassword);
        if (user!= null) {
            request.setAttribute("loginUser", user);
            return "main";
        } else {
            request.setAttribute("message", "用户名或密码错误");
            return "login";
        }
    }
    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("loginUser");
        return "login";
    }
    @GetMapping(value = "/user/list")
    public String findUsers(String usrName, Long roleId, Model model) {
        List<User> list=userService.findAllUsers();
        model.addAttribute("users",list);
        return "/user/list";
    }

    @GetMapping("/getUser")
    public User getUser(Integer usrId) {
        User user = userService.getUserById(usrId);
        return user;
    }


    @GetMapping("/Users")
    public List<User> getUsers() {
        return userService.findAllUsers();
    }


    @GetMapping("user/{id}")
    public User getUserById(@PathVariable Integer id) {
        return userService.getUserById(id);
    }


    @GetMapping("/add")
    public String addUser(Model model) {
        // 获取所有角色列表
        List<Role> roles = roleService.list();
        // 将角色列表添加到模型中，以便在页面中展示
        model.addAttribute("roles", roles);
        return "/user/add";
    }

    @PostMapping("/save")
    public String saveUser(User user) {
        int i = userService.saveUser(user);
        if (i > 0) {
            return "redirect:/user/list";
        }
        return "/user/add";
    }


    @GetMapping("/check")
    @ResponseBody
    public String checkUser(String usrName) {
        if (usrName!= null &&!"".equals(usrName)) {
            return "false";
        }
        List<User> list = userService.list(new QueryWrapper<User>().eq("usr_name", usrName));
        if (list.size() > 0) {
            return "false";
        } else {
            return "true";
        }
    }


    @GetMapping("/edit")
    public String updateUser(Model model, Long usrId) {
        // 获取所有角色列表
        List<Role> roles = roleService.list();
        // 根据用户 ID 获取用户信息
        User user = userService.getUserById(Math.toIntExact(usrId));
        // 将角色列表和用户信息添加到模型中，以便在页面中展示
        model.addAttribute("roles", roles);
        model.addAttribute("user", user);
        return "/user/edit";
    }


    @PostMapping("/edit")
    public String editUser(User user) {
        int i = userService.saveUser(user);
        if (i > 0) {
            return "redirect:/user/list";
        }
        return "/user/edit";
    }


    @RequestMapping("/list")
    public String list(Model model, String usrName, @RequestParam(required = false, defaultValue = "0") Integer roleId,
                       @RequestParam(required = false, defaultValue = "1") int pageIndex) {
        Sort sort = Sort.by(Sort.Direction.ASC, "usrId");
        Pageable pageable= PageRequest.of(pageIndex - 1, 5, sort);
        return "user/list";
    }
}
